const app = getApp();

Page({
  data: {
    shopInfo: {
      name: '',
      description: '',
      logo: ''
    },
    logoTempPath: '', // 临时Logo路径
    submitting: false
  },
  
  onLoad: function() {
    // 检查登录状态
    if (!app.globalData.isLogin) {
      wx.redirectTo({
        url: '/pages/login/login'
      });
    }
  },
  
  // 店铺名称输入
  inputShopName: function(e) {
    this.setData({
      'shopInfo.name': e.detail.value
    });
  },
  
  // 店铺描述输入
  inputShopDesc: function(e) {
    this.setData({
      'shopInfo.description': e.detail.value
    });
  },
  
  // 选择店铺Logo
  chooseLogo: function() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFilePath = res.tempFilePaths[0];
        this.setData({
          logoTempPath: tempFilePath
        });
      }
    });
  },
  
  // 提交创建店铺
  submitShop: function() {
    // 表单验证
    if (!this.data.shopInfo.name) {
      wx.showToast({
        title: '请输入店铺名称',
        icon: 'none'
      });
      return;
    }
    
    this.setData({ submitting: true });
    
    // 先上传图片（如果有）
    if (this.data.logoTempPath) {
      this.uploadLogo();
    } else {
      this.createShop();
    }
  },
  
  // 上传Logo
  uploadLogo: function() {
    wx.uploadFile({
      url: 'https://api.example.com/upload',
      filePath: this.data.logoTempPath,
      name: 'file',
      header: {
        'Authorization': `Bearer ${app.globalData.token}`
      },
      success: (res) => {
        const data = JSON.parse(res.data);
        if (data.url) {
          this.setData({
            'shopInfo.logo': data.url
          });
          this.createShop();
        } else {
          wx.showToast({
            title: '上传Logo失败',
            icon: 'none'
          });
          this.setData({ submitting: false });
        }
      },
      fail: () => {
        wx.showToast({
          title: '上传Logo失败',
          icon: 'none'
        });
        this.setData({ submitting: false });
      }
    });
  },
  
  // 创建店铺
  createShop: function() {
    wx.request({
      url: 'https://api.example.com/shop/create',
      method: 'POST',
      header: {
        'Authorization': `Bearer ${app.globalData.token}`
      },
      data: this.data.shopInfo,
      success: (res) => {
        if (res.data && res.data.success) {
          // 创建成功
          app.globalData.shopInfo = this.data.shopInfo;
          app.globalData.hasShop = true;
          
          wx.showToast({
            title: '创建成功',
            icon: 'success'
          });
          
          // 跳转到首页
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/index/index'
            });
          }, 1500);
        } else {
          wx.showToast({
            title: res.data.message || '创建失败',
            icon: 'none'
          });
          this.setData({ submitting: false });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
        this.setData({ submitting: false });
      }
    });
  }
}) 